import { warrantyCategoryFormSave, getOrderWarrantyCategoryDetail } from '@/api/order'
import { ref } from 'vue'

/**
 * 质保类别表单
 */
export default () => {
  // 表单保存加载
  const formSaveLoading = ref(false)

  // 设置表单加载
  const setFormLoading = ref(false)

  // 表单
  const form = ref<OrderAPIRequest.WarrantyCategoryForm>({
    checked: false,
    collectFee: false,
    defaultSelect: false,
    status: false,
    id: '',
    name: '',
    type: 1,
  })

  /**
   * 表单保存
   */
  const formSave = async () => {
    formSaveLoading.value = true
    const res = await warrantyCategoryFormSave(form.value)
    formSaveLoading.value = false
    return res.data
  }

  /**
   * 设置表单
   * @param {String} id - 质保类别id
   */
  const setForm = async (id: string) => {
    setFormLoading.value = true
    const res = await getOrderWarrantyCategoryDetail(id)
    setFormLoading.value = false
    if (res.data.code === 20000) {
      form.value = res.data.data.row
      return ''
    }
    return res.data.message
  }

  /**
   * 重置表单
   */
  const resetForm = () => {
    form.value = {
      checked: false,
      collectFee: false,
      defaultSelect: false,
      status: false,
      id: '',
      name: '',
      type: 1,
    }
  }

  return {
    form,
    setFormLoading,
    formSaveLoading,
    setForm,
    formSave,
    resetForm,
  }
}
